package com.xhtech.base.proxy

import android.content.Context
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.bitmap.GranularRoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.yxh.lib_base.util.image.IImageLoadEngine
import com.yxh.lib_base.util.image.ImageLoaderOptions

/**
 * 图片加载引擎
 * */
class XhGlideEngine : IImageLoadEngine {
    override fun load(any: Any?, imageView: ImageView) {
        super.load(any, imageView)
        Glide.with(imageView).load(any).into(imageView)
    }


    override fun load(
        imageView: ImageView,
        any: Any?,
        options: (ImageLoaderOptions.Builder.() -> Unit)?
    ) {
        if (null != options) {
            var builder = ImageLoaderOptions.Builder()
            options.invoke(builder)
            var glideOptions = builder.build().buildGlideOptions(imageView.context)
            Glide.with(imageView).load(any).apply(glideOptions).into(imageView)
        } else {
            Glide.with(imageView).load(any).into(imageView)
        }
    }

    private fun ImageLoaderOptions.buildGlideOptions(context: Context): RequestOptions {
        var mRequestOptions = RequestOptions()
        //是否跳过内存
        var requestOptions = RequestOptions.skipMemoryCacheOf(isSkipMemoryCache)
        //占位符
        if (holderDrawable != null) {
            requestOptions = requestOptions.placeholder(holderDrawable)
        }
        if (holderDrawableId !== -1) {
            requestOptions = requestOptions.placeholder(holderDrawableId)
        }
        //错误图
        if (errorDrawableId !== -1) {
            requestOptions = requestOptions.error(errorDrawableId)
        }
        //isCenterCrop
        if (isCenterCrop) {
            requestOptions = requestOptions.centerCrop()
        }
        //isCircle-圆
        if (isCircle) {
            requestOptions = requestOptions.optionalCircleCrop()
        }
        //圆角
        if (radiusList != null && radiusList.isNotEmpty()) {
//            requestOptions = requestOptions.transform(new GlideRoundTransform(context,  dip2px(context, getmRadius()),getRoundFilter()));
            requestOptions = requestOptions.transform(
                GranularRoundedCorners(
                    radiusList[0],
                    radiusList[1],
                    radiusList[2],
                    radiusList[3]
                )
            )
        } else if (radiuas != 0f) {
            requestOptions = requestOptions.transform(
                GranularRoundedCorners(radiuas, radiuas, radiuas, radiuas)
            )
        }
        /**
         * 高斯模糊效果
         */
        if (blur > 0) {
//            requestOptions = RequestOptions.bitmapTransform(
//                GlideBlurTransformation(blur, getmBlurSampling())
//            )
        }
        //mIsSkipDiskCacheCache
        requestOptions = if (isSkipDiskCacheCache) {
            requestOptions.diskCacheStrategy(DiskCacheStrategy.NONE)
        } else {
            //默认使用智能策略
            requestOptions.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
        }
//        //thumbnail
//        if (thumbnail !== 1f) {
//            mRequestBuilder.thumbnail(thumbnail)
//        }
        //override
        val overridePoint = overridePoint
        if (overridePoint.x != 0 && overridePoint.y != 0) {
            requestOptions = requestOptions.override(overridePoint.x, overridePoint.y)
        }

        return requestOptions
    }


}